<?php

namespace App\Http\Controllers;

use App\Bug;
use App\Http\Requests\BugCreateRequest;
use App\Services\UploadsManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BugsController extends Controller
{
    protected $manager;
    public function __construct(UploadsManager $manager)
    {
        $this->middleware('auth');
        $this->manager = $manager;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data=Bug::orderBy('status','asc')->orderBy('created_at','desc')->get();
        return view('bugs.index')->with('data',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('bugs.cerate');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(BugCreateRequest $request)
    {
        $data=$request->all();
        $result=Bug::create($data);
        if ($result){
            return redirect()
                ->route('bugs.index')
                ->with('success', '反馈BUG成功.');
        }else{
            return redirect()
                ->route('bugs.create')
                ->with('error', '反馈BUG失败.');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data=Bug::find($id);
        return view('bugs.show')->with('data',$data);
    }
    /**
     * 上传文件
     */
    public function uploadFile(Request $request)
    {
        $file = $_FILES['file'];
        $fileName = $request->get('file_name');
        $fileName = $fileName ?: $file['name'];
        $path = str_finish($request->get('folder'), '/public/') . $fileName;
        $content = \Illuminate\Support\Facades\File::get($file['tmp_name']);
        $result = $this->manager->saveFile($path, $content);

        if ($result === true) {
            return redirect()
                ->back()
                ->with("success", '文件「' . $fileName . '」上传成功.')
                ->with("name",$fileName);
        }
        $error = $result ?: "文件上传出错.";
        return redirect()
            ->back()
            ->withErrors([$error]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data=$request->all();
        $bug=Bug::findOrFail($id);
        if ($data['status']==1){
                if ($bug->recipient==''){
                    $bug->fill($data);
                    $bug->save();
                    return redirect()
                        ->route('bugs.index')
                        ->with('success', '请尽快解决此bug');

                }elseif($bug->recipient!=Auth::user()->id){
                    return redirect()
                        ->back()
                        ->with('error','已有人选择挑战此bug，请重新选择');
                }else{
                    return redirect()
                        ->back()
                        ->with('success','你已选择挑战此bug，请勿重复选择');
                }
        }else{
            $bug->fill($data);
            $bug->save();
            return redirect()
                ->route('consoles')
                ->with('success', '期待你的下次尝试');
        }
    }
    public function downloadFile($path){
        $files=__FILESRC__.'/'.$path;
        $name = basename($files);
        // basename() 函数返回路径中的文件名部分。
        return response()->download($files,$name);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $bugs = Bug::findOrFail($id);
        $bugs->delete();
        if ($bugs->trashed()) {
            return redirect()
                ->route('consoles')
                ->with('success', '恭喜你，挑战成功！.');
        }
    }
}
